package com.tom.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tom.config.CustomResponse;
import com.tom.entity.TomArticle;
import com.tom.vo.TomContentJson;
import com.tom.dto.UpdateArticleDTO;
import com.tom.mapper.opencms.TomArticleMapper;
import com.tom.service.IArticleService;
import com.tom.service.IbatchService;

import java.util.List;


/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @version V1.0
 * @Package bw-article-api
 * @Description:
 * @author: dengyi
 * @date: 2020/2/10
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 */
@Slf4j
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IbatchService batchService;
    @Autowired
    TomArticleMapper tomArticleMapper;
    /**
     * 清理DB无法Json无法转换成Html数据
     */
    @PostMapping(value = "/deleteJsonTransitionHtmlFail")
    public CustomResponse deleteJsonTransitionHtmlFail(@RequestBody TomArticle tomArticle){

        String messages= articleService.deleteJsonTransitionHtmlFail(tomArticle);
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", messages);
    }

    /**
     * 给tom_article_price补齐tom_article_id字段
     */
    @GetMapping("/insertTomArticleIdToTomAriclePrice")
    public void   insertTomArticleIdToTomAriclePrice(){
        Integer size=0;
         while (true){
            //  Integer skipSize=size-1000;
            List<TomContentJson> tomContentJsons= tomArticleMapper.selectAriclePath(size);
            //跳过1000条
            if (tomContentJsons==null||tomContentJsons.size()<1){
                log.info("同步结束，退出循环,最后一个：Content_json_id"+size);
                break;
            }
            //TODO 更新操作
            for (TomContentJson tomContentJson : tomContentJsons) {
                //查询tomArcileId
                Integer articleId= tomArticleMapper.selectAricleIdByAriclePath(tomContentJson.getArticlePath());
                //更新这1000条
                tomArticleMapper.updateAricleIdByContentJsonId(articleId,tomContentJson.getContent_json_id());
            }
            size=tomContentJsons.get(tomContentJsons.size()-1).getContent_json_id();
         }
        log.info("执行完毕，循环结束");


    }


    /**
     * 更新
     *
     * @param article
     * @return
     */
    @PostMapping(value = "/updateArticle")
    public CustomResponse listArticle(@RequestBody UpdateArticleDTO article) {
        String[] ids = article.getArticleNo().split(",");
        StringBuffer messages = new StringBuffer();
        for (String id : ids) {
            String message = articleService.getArticleContent("", article.getType(), id, article.getSiteNo(), article.getState(), false);
            messages.append("id:" + id + "-" + message + "; ");
        }
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", messages);
    }

    /**
     * 通过时间更新文章
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/updateArticleByDate")
    public CustomResponse updateArticleByDate(String startDate, String endDate) {
        String message = articleService.updateArticleByDate(startDate, endDate);
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", message);
    }
    /**
     * 通过时间更新文章
     *startDate 文章发布开始时间
     * endDate 文章发布结束时间
     * type 1 免费 2 付费
     * @param
     * @return
     */
    @RequestMapping(value = "/updateArticleByDateAndType")
    public CustomResponse updateArticleByDateAndType(String startDate, String endDate,Integer type) {
        String message = articleService.updateArticleByDateAndType(startDate, endDate,type);
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", message);
    }
    /**
     * 通过时间更新文章
     *startDate 文章发布开始时间
     * endDate 文章发布结束时间
     * type 1 免费 2 付费
     * @param
     * @return
     */
    @RequestMapping(value = "/updateDBArticleByDate")
    public CustomResponse updateDBArticleByDate(String startDate, String endDate) {
        String message = articleService.updateDBArticleByDate(startDate, endDate);
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", message);
    }
    /**
     * 拆分字段，使用一次就好
     */
    @RequestMapping(value = "/updateArticleAccessParametersByDate")
    public CustomResponse updateArticleAccessParameters(String startDate, String endDate) {
        articleService.updateArticleAccessParameters(startDate,endDate);
        return CustomResponse.create().body("CODE", "200");
    }

    /**
     * 强制更新所有
     *
     * @return
     */
    @RequestMapping(value = "/mandatoryRefresh")
    public CustomResponse mandatoryRefresh() {
        String message = batchService.mandatoryRefresh();
        return CustomResponse.create().body("CODE", "200").body("MESSAGE", message);
    }

    /**
     * 按年份录入付费文章
     *
     * @param year
     * @param start
     * @param end
     * @return
     */
    @RequestMapping(value = "/payListArticle")
    public CustomResponse payListArticle(Integer year, Integer start, Integer end) {
        if (null != year && start != null && end != null) {
            articleService.chargeListArticle(year, start, end);
            return CustomResponse.ok().body("MESSAGE", "已完成" + year + "年的付費文章");
        }
        return CustomResponse.create().body("CODE", "201").body("MESSAGE", "參數不對");

    }

    @RequestMapping(value = "/payListArticleNotSkip")
    public CustomResponse payListArticleNotSkip(Integer year, Integer start, Integer end,String provenance ) {
        if (null != year && start != null && end != null) {
            articleService.chargeListArticleNotSkip(year, start, end,provenance);
            return CustomResponse.ok().body("MESSAGE", "已完成" + year + "年的付費文章");
        }
        return CustomResponse.create().body("CODE", "201").body("MESSAGE", "參數不對");
    }
}
